package com.sd.repay.task;

import com.sd.repay.common.Constants;
import com.sd.repay.pojo.TransferOrder;
import com.sd.repay.pojo.WithdrawOrder;
import com.sd.repay.service.TransferOrderService;
import com.sd.repay.service.WithdrawOrderService;
import com.sd.repay.utils.WebUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 同步统一出账表订单状态任务
 * Created by sd on 2018/08/28.
 */
@Component
public class SyncWithdrawOrderTask {
    private static Logger log = LoggerFactory.getLogger(SyncWithdrawOrderTask.class);

    @Autowired
    private WithdrawOrderService withdrawOrderService;
    @Autowired
    private TransferOrderService transferOrderService;

    /**
     * 根据统一出账表未知或交易中的订单去对应通道同步状态
     */
    public void executeSyncWithdrawOrder() {
        log.info("--------上游同步订单状态定时任务开始执行---------");
        try {
            List<WithdrawOrder> unEndStatusOrders = withdrawOrderService.getUnEndStatusWithdrawOrder();
            if (CollectionUtils.isEmpty(unEndStatusOrders)) {
                log.info("没有需要同步状态的统一出账订单");
                return;
            }
            log.info("--------需要同步状态的统一出账订单记录数-------------" + unEndStatusOrders.size());

            TransferOrder transferOrder = null;
            String withdrawOrderNo = "", service = "", transferOrderNo = "", transferOrderStatus = "", transferResCode = "", transferResMsg = "";

            for (WithdrawOrder withdrawOrder : unEndStatusOrders) {

                withdrawOrderNo = withdrawOrder.getOrderNo();
                service = withdrawOrder.getService();

                transferOrder = transferOrderService.getTransferOrderBySource(withdrawOrderNo, service);
                if (null == transferOrder) {
                    log.info("统一出账订单号" + withdrawOrderNo + "对应出款平台记录不存在，故修改状态为失败");

                    int successUpdateRow = withdrawOrderService.writeBackWithdrawInfo(withdrawOrderNo, Constants.WITHDRAW_STATUS_FAIL, null, null, "", "对应平台出账记录不存在");
                    log.info("统一出账订单号" + withdrawOrderNo + "对应出款平台订单不存在，修改状态失败影响的行数successUpdateRow=" + successUpdateRow);
                    successUpdateRow = withdrawOrderService.updateWithdrawOrderNotifyStatus(withdrawOrderNo, Constants.IS_NEED_NOTIFY);
                    log.info("统一出账订单号" + withdrawOrderNo + "对应出款平台订单不存在，修改需要通知状态影响的行数successUpdateRow=" + successUpdateRow);

                    continue;
                }

                transferOrderNo = transferOrder.getSubOrderNo();
                transferOrderStatus = transferOrder.getStatus();
                transferResCode = transferOrder.getResultCode();
                transferResMsg = transferOrder.getResultMsg();

                log.info("统一出账订单号" + withdrawOrderNo + "对应出款平台记录transferOrderNo:{},transferOrderStatus:{}", new Object[]{transferOrderNo, transferOrderStatus});
                if (Constants.TRANSFER_STATUS_SUBMIT_FAIL.equals(transferOrderStatus) || Constants.TRANSFER_STATUS_FAIL.equals(transferOrderStatus)) {

                    int successUpdateRow = withdrawOrderService.writeBackWithdrawInfo(withdrawOrderNo, Constants.WITHDRAW_STATUS_FAIL, null, transferOrderNo, transferResCode, transferResMsg);
                    log.info("统一出账订单号" + withdrawOrderNo + "对应出款平台订单号为失败，修改状态影响的行数successUpdateRow=" + successUpdateRow);
                    successUpdateRow = withdrawOrderService.updateWithdrawOrderNotifyStatus(withdrawOrderNo, Constants.IS_NEED_NOTIFY);
                    log.info("统一出账订单号" + withdrawOrderNo + "对应出款平台订单号为失败，修改需要通知状态影响的行数successUpdateRow=" + successUpdateRow);

                } else if (Constants.TRANSFER_STATUS_SUCCESS.equals(transferOrderStatus)) {

                    int successUpdateRow = withdrawOrderService.writeBackWithdrawInfo(withdrawOrderNo, Constants.WITHDRAW_STATUS_SUCCESS, null, transferOrderNo, transferResCode, transferResMsg);
                    log.info("统一出账订单号" + withdrawOrderNo + "对应出款平台订单号为成功，修改状态影响的行数successUpdateRow=" + successUpdateRow);
                    successUpdateRow = withdrawOrderService.updateWithdrawOrderNotifyStatus(withdrawOrderNo, Constants.IS_NEED_NOTIFY);
                    log.info("统一出账订单号" + withdrawOrderNo + "对应出款平台订单号为成功，修改需要通知状态影响的行数successUpdateRow=" + successUpdateRow);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("-----------同步统一出账订单状态定时任务完成--------------");
        WebUtil.removeBlockTaskBySpringId(WebUtil.getClassSimpleName(this.getClass()));
    }

}
